using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.ApplicationPages;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Utilities;

namespace MOSS.CustomAdminPages.UserMembership.UI
{
    public class ViewUMListPage : WebAdminPageBase
    {
        #region Constructor
        public ViewUMListPage()
        {

        }
        #endregion

        #region Variables
        protected Literal litlist;
        private const string LISTVIEWPAGE = "ViewUMList.aspx";
        private const string EDITITEMPAGE = "EditUM.aspx";

        protected TextBox txtLogin;
        #endregion

        #region Methods / Properties
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            
            bool bAlt = true;

            string userLogin = Request.QueryString["login"] != null ? Request.QueryString["login"].ToString() : null;

            using (SPWeb web = SPContext.GetContext(this.Context).Web)
            {
                SPUser user = null;
                
                StringBuilder sb = new StringBuilder();
                if (string.IsNullOrEmpty(txtLogin.Text))
                {
                    if (string.IsNullOrEmpty(userLogin))
                    {
                        txtLogin.Text = web.CurrentUser.LoginName;
                        user = web.CurrentUser;
                    }
                    else
                    {
                        txtLogin.Text = userLogin;
                    }
                    
                }

                if (user == null)
                {
                    if (txtLogin.Text != "*")
                    {
                        user = web.SiteUsers[txtLogin.Text];

                        if (user != null)
                        {
                            RenderUser(bAlt, user, sb);
                        }
                    }
                    else
                    {
                        foreach (SPUser userToRender in web.SiteUsers)
                        {
                            if (!userToRender.LoginName.StartsWith("SHAREPOINT") &&
                                !userToRender.LoginName.StartsWith("NT AUTHORITY")
                                )
                            {
                                RenderUser(bAlt, userToRender, sb);
                                bAlt = !bAlt;
                            }
                        }
                    }
                }
                else
                {
                    RenderUser(bAlt, user, sb);
                }

                litlist.Text = sb.ToString();
            }

        }

        private void RenderUser(bool bAlt, SPUser user, StringBuilder sb)
        {
            #region Render Lists
            //Display List Groups
            if (user.Groups != null)
            {
                string editurl = "";
                editurl = EDITITEMPAGE + "?login=" + user.LoginName;

                string groups = "";
                StringBuilder sbGroups = new StringBuilder();
                bool firstItem = true;
                foreach (SPGroup group in user.Groups)
                {
                    if (!firstItem)
                        sbGroups.Append(", ");

                    sbGroups.Append(group.Name);
                    firstItem = false;
                }

                groups = sbGroups.ToString();

                sb.Append(RenderUserMembership(user, groups, editurl, bAlt));
            }
            #endregion
        }

        private string RenderUserMembership(SPUser user, string groups, string EditUrl, bool bAlternating)
        {
            string action = null;

            if (EditUrl.Length == 0)
                action = ""; 
            else
                action = "<a href='" + EditUrl + "'>" + user.LoginName + "</a>";

            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("<TR class='{0}'>", bAlternating ? "ms-alternating" : string.Empty);
            sb.Append("<TD class='ms-vb2'>");
            sb.Append(user.Name);
            sb.Append("</TD>");
            sb.Append("<TD class='ms-vb2'>");
            sb.Append(action);
            sb.Append("</TD>");
            sb.Append("<TD class='ms-vb2'>");
            sb.Append(user.Email);
            sb.Append("</TD>");
            sb.Append("<TD class='ms-vb2'>");
            sb.Append(groups);
            sb.Append("</TD>");
            sb.Append("</TR>");

            return sb.ToString();
        }

        private string RenderTitle(string title)
        {
          return @"<TR class=''><TD class='ms-vb2' style='padding-top:3px; padding-left:4px;'><B><h3 class='ms-standardheader'>" + title + "</h3></B></TD></TR>";
        }

        #endregion
    }
}




